﻿import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from entities import *
from entities import Entities
from Entities import *
#from entities import NganhNgheNguoiDungEntities
#from entities import NganhNgheEntities
def Get(keyNguoiDung=None, LinhVucID=0):
    linhvuc_list = []
    results = LinhVuc.all()
    if keyNguoiDung!= None:
        try:
            nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
        except:
            return "Invalid Key"

        # neu 0 la Admin :D 
        if keyNguoiDung != Entities.GetKey():
            linhvucID_list = NganhNgheEntities.GetLinhVucID(keyNguoiDung)
            results = results.filter("__key__ IN", linhvucID_list)
    
    if LinhVucID > 0:
        linhvuc = LinhVuc.get_by_id(LinhVucID)
        linhvuc_dict ={}
        linhvuc_dict['LinhVucID'] = linhvuc.key().id()
        linhvuc_dict['TenLinhVuc'] = linhvuc.TenLinhVuc 
        linhvuc_dict['HoatDong'] = 0
        linhvuc_list.append(linhvuc_dict)
    else:
        for linhvuc in results:
            linhvuc_dict ={}
            linhvuc_dict['LinhVucID'] = linhvuc.key().id()
            linhvuc_dict['TenLinhVuc'] = linhvuc.TenLinhVuc 
            linhvuc_dict['HoatDong'] = 1
            linhvuc_list.append(linhvuc_dict)
    tuple_linhvuc = tuple(linhvuc_list)  
    return tuple_linhvuc
 
def GetAll(keyNguoiDung):
    tuple_linhvucAll =  NganhNgheEntities.GetLinhVucID(keyNguoiDung) 
    linhvuc_nguoidung = []
    linhvuc_list = []
    for tp in tuple_linhvucAll:
        linhvuc_nguoidung.append(tp.id())
    results = LinhVuc.all()
    for linhvuc in results:
        linhvuc_dict = {}
        linhvuc_dict['LinhVucID'] = linhvuc.key().id()
        linhvuc_dict['TenLinhVuc'] = linhvuc.TenLinhVuc
        if linhvuc.key().id() in linhvuc_nguoidung:
            linhvuc_dict['HoatDong'] = 1
        else:
            linhvuc_dict['HoatDong'] = 0
        linhvuc_list.append(linhvuc_dict)        
    tuple_linhvuc = tuple(linhvuc_list)  
    return tuple_linhvuc
    
def All():
    linhvucs = LinhVuc.all()
    linhvuc_list = []
    for linhvuc in linhvucs:
        linhvuc_dict = {}
        linhvuc_dict['LinhVucID'] = linhvuc.key().id()
        linhvuc_dict['TenLinhVuc'] = linhvuc.TenLinhVuc
        linhvuc_list.append(linhvuc_dict)
    tuple_linhvuc = tuple(linhvuc_list)
    return tuple_linhvuc
    
def Add_Admin(TenLinhVuc): 
    linhvuc = db.GqlQuery("SELECT * FROM LinhVuc WHERE TenLinhVuc = :1",
                              TenLinhVuc)
    if linhvuc.count() == 0:                      
        linhvuc = LinhVuc(TenLinhVuc=TenLinhVuc)
        linhvuc.put()         
        return "Complete"
    else:
        return "Duplicate"   

def Add(keyNguoiDung, LinhVucID):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    linhvuc = LinhVuc.get_by_id(LinhVucID)
    nganhnghes = Entities.NganhNghe.all().filter("LinhVucID",linhvuc)        
    for nn in nganhnghes:    
        NganhNgheNguoiDungEntities.Add(keyNguoiDung, nn.key().id())
    return "Complete"    

def Update(LinhVucID,TenLinhVuc):
    
    linhvuc_update = LinhVuc.get_by_id(LinhVucID)
    linhvuc = db.GqlQuery("SELECT * FROM LinhVuc WHERE TenLinhVuc = :1",TenLinhVuc)
    if linhvuc.count() == 0:
        linhvuc_update.TenLinhVuc = TenLinhVuc
        linhvuc_update.put()
        return "Complete"
    else:
        return "Duplicate"

           
def Delete(keyNguoiDung, LinhVucID):
    return NganhNgheEntities.Delete(keyNguoiDung,0,LinhVucID)
        
def GetKey(keyNguoiDung):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    linhvuc_list = []
    
    linhvucs = LinhVuc.all().filter("NguoiDung =", nguoidung)
    for linhvuc in linhvucs:
        linhvucID = linhvuc.key().id()
    return linhvucID
    
def Test():
    results =  LinhVuc.all()
    linhvuc_list = []
    for linhvuc in results:
        linhvuc_dict ={}
        linhvuc_dict['LinhVucID'] = linhvuc.key().id()
        linhvuc_dict['TenLinhVuc'] = linhvuc.TenLinhVuc 
        linhvuc_dict['HoatDong'] = 1
        linhvuc_list.append(linhvuc_dict)  
    tuple_linhvuc = tuple(linhvuc_list)
    return tuple_linhvuc